package com.soft.cake.controller;

import java.io.IOException;
import java.io.Writer;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.soft.cake.pojo.Cart;
import com.soft.cake.pojo.Img;
import com.soft.cake.pojo.Product;
import com.soft.cake.pojo.User;
import com.soft.cake.service.ICartService;
import com.soft.cake.service.IImgService;

/**
 * @description 购物车的Controller
 * @author Bryan
 */

@Controller
@RequestMapping("/cart")
public class CartController {
	
	@Autowired
	private ICartService cartService;
	@Autowired
	private IImgService imgService;
	
	/**
	 * @description 增加到购物车,配合前台ajax使用
	 * @author Bryan
	 * @param id 前台传过来的商品id quantity 前台传递过来的商品目前的数量
	 * @return
	 * @throws IOException 
	 */
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public void add(int id, int quantity, HttpServletResponse resp, HttpSession session) throws IOException {
		//从session中获取当前的登入的用户
		User user = (User)session.getAttribute("loginUser");
		
		/**
		 * 如果当前用户为空,说明没有登登录
		 * 记录下当前用户的当前URI,用户登录后跳转回该网页
		 * 前台控制页面跳转到登录页面
		 * 2019/8/24 15：05 登录后没有放在session中
		 * 模拟用户已经登录
		 */
		
		//*******模拟用户**********
		user = new User();
		user.setUserId(1);
		//*****************
		
		//这里报警告是正常的  因为已经模拟了用户 不可能为空
		if(user == null) {
			session.setAttribute("URI", "product/" + id + "/item");
			resp.sendRedirect("");
			return;
		}
		else {
			// if user has been login
			Product product = new Product();
			product.setProductId(id);
			Cart cart = new Cart(product,user,quantity);
			cartService.add(cart);
			Writer out = null;
			try {
				out = resp.getWriter();
				out.write("yes");
			} finally {
				out.close();
			}
		}
	}
	
	/**
	 * @description 购物车列表
	 * @author Bryan
	 * @param id 为登入用户的id 
	 * @return
	 */
	@RequestMapping(value="/{id}/list",method=RequestMethod.GET)
	public String list(@PathVariable int id, Model model) {
		List<Cart> carts = cartService.loadList(id);
		//创建一个Map存储购物车中的商品id和图片的url
		Map<Integer, String> productImgMap = new HashMap<Integer, String>();
		/*List<Img> imgs =*/
		/**
		 * 取出购物车中每个产品的首张图片
		 */
		for(Cart cart:carts) {
			int productId = cart.getProduct().getProductId();
			Img img = imgService.imgList(productId).get(0);
			String imgUrl = img.getImgUrl();
			productImgMap.put(productId, imgUrl);
		}
		
		model.addAttribute("carts",carts);
		model.addAttribute("productImgMap", productImgMap);
		return "checkout";
	}

	/**
	 * 
	 * @author Bryan
	 * @Description 根据购物车的id删除商品
	 * @param id 为购物车id
	 * @return
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
	public String delete(@PathVariable Integer id) {
		cartService.delete(id);
		return "checkout";
	}
	
	/**
	 * 
	 * @author Bryan
	 * @Description 
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(HttpServletRequest request, HttpSession session) {
		int id = Integer.valueOf(request.getParameter("cartId"));
		int quantity = Integer.valueOf(request.getParameter("quantity"));
		System.out.println("商品id"+id);
		System.out.println("现在数量:"+quantity);
		cartService.update(id,quantity);
		return "index";
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public String delete(HttpServletRequest request, HttpSession session) {
		int id = Integer.valueOf(request.getParameter("cartId"));
		System.out.println("商品id"+id);
		/*cartService.delete(id);*/
		return "index";
	}
	
}

	
